package com.booth.base.model;

import java.util.List;

import com.booth.base.utils.MD5;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;

/***
 * 用户
 * 
 * @TypeName：Users @description：
 * @author：Administrator
 */
public class Users extends Model<Users> {
	private static final long serialVersionUID = 1L;
	public static final Users dao = new Users();
	private List<Integer> moduleIds;
	private List<String> actionUrls;

	/***
	 * 根据过滤条件获取数据，并分页显示
	 * 
	 * @param pageNumber：当前页
	 * @param pageSize：每页条数
	 * @param userName：用户数
	 * @param loginName：登录名称
	 * @param groupId：组id
	 * @param sex：性别
	 * @param status：状态
	 * @return
	 * @throws Exception
	 */
	public static Page<Users> findPage(int pageNumber, int pageSize, String userName, String loginName, String groupId,
			String sex, String status) throws Exception

	{
		String sql = " where 1=1 ";
		if (userName != null && !userName.equals("")) {
			//由于中文会引起乱码，所以进行编码格式化
			userName = new String(userName.getBytes(), "UTF-8");
			sql += " and u.user_name like concat('%" + userName + "%')";
		}
		if (loginName != null && !loginName.equals("")) {
			//由于中文会引起乱码，所以进行编码格式化
			loginName = new String(loginName.getBytes(), "UTF-8");
			sql += " and u.login_name like concat('%" + loginName + "%')";
		}
		if (groupId != null && !groupId.equals("")) {
			sql += " and u.group_id = " + groupId;
		}
		if (sex != null && !sex.equals("")) {
			//由于中文会引起乱码，所以进行编码格式化
			sex = new String(sex.getBytes(), "UTF-8");
			sql += " and u.sex = '" + sex + "'";
		}
		if (status != null && !status.equals("")) {

			sql += " and u.status =" + status;
		}
		sql += "  order by user_id asc";
		return dao.paginate(pageNumber, pageSize, "select u.*,g.group_name ",
				" from users u inner join groups g on u.group_id=g.id " + sql);
	}

	/***
	 * 校验登录
	 * 
	 * @param loginName：登录名称
	 * @param loginPwd：登录密码
	 * @return
	 */
	public static Users doLogin(String loginName, String loginPwd) {
		String pwd = MD5.md5(loginPwd).substring(4, 28);

		List lst = dao.find(
				"SELECT t.user_id,t.user_name,t.group_id,a.group_type,a.group_name FROM users t\n"
				+ "left join groups a on t.group_id = a.id and a.status = 9\n"
				+ " WHERE t.login_name=? AND t.password=? AND t.status=9 limit 0,1",
				new Object[] { loginName, pwd });
		if (lst.size() > 0) {
			Users u = (Users) lst.get(0);
			Db.update("UPDATE users SET last_time=now() WHERE user_id=" + u.getInt("user_id"));
			return u;
		}

		return null;
	}

	/***
	 * 修改密码
	 * 
	 * @param uid
	 * @param newPwd
	 * @return
	 */
	public boolean changePassword(Integer uid, String newPwd) {
		int ret = Db.update("update users set password=? where user_id=?", new Object[] { newPwd, uid });

		return ret > 0;
	}

	public List<Integer> getModuleIds() {
		return this.moduleIds;
	}

	public void setModuleIds(List<Integer> moduleIds) {
		this.moduleIds = moduleIds;
	}

	public List<String> getActionUrls() {
		return this.actionUrls;
	}

	public void setActionUrls(List<String> actionUrls) {
		this.actionUrls = actionUrls;
	}

	private Integer userId;
	
	private String userName;

	public Integer getUserId() {
		return userId;
	}

	public void setUserId(Integer userId) {
		this.userId = userId;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}



	private Integer groupType;
	
	private String groupName;
	
	
	public Integer getGroupType() {
		return groupType;
	}

	public void setGroupType(Integer groupType) {
		this.groupType = groupType;
	}
	
	

	public String getGroupName() {
		return groupName;
	}

	public void setGroupName(String groupName) {
		this.groupName = groupName;
	}

	public Long checkUniqueLoginName(String loginName) {
		return Db.queryLong("select count(1) from users t where t.login_name = '"+loginName+"'");
	}

}